package com.softaria.windows.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.softaria.gwt.common.client.widget.Href;
import com.softaria.windows.styles.TopMenuStyles;

/**
 * Represents top menu on {@link Desktop}
 * 
 * @author Roman M Kuzmin (roman@softaria.com)
 * 
 */
public class TopMenu extends FlexTable implements TopMenuStyles {

  private HorizontalPanel accountMenu;

  public TopMenu() {

    setLogoWidget(new Image(GWT.getModuleBaseURL() + "windows/images/logo.png"));

    accountMenu = new HorizontalPanel();
    setWidget(0, 1, accountMenu);

    getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT);
    getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);

    getElement().setId(TOP_MENU_ID);
  }

  /**
   * Let you to set logo widget in left top corner of {@link Desktop}
   * 
   * @param logo
   */
  public void setLogoWidget(Widget logo) {
    setWidget(0, 0, logo);
    getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
  }

  /**
   * Let you add clickable top menu points
   * 
   * @param point
   */
  public void addMenuPoint(Hyperlink point) {

    addMenuWidget(point);
  }

  private void addMenuWidget(Widget point) {
    if (accountMenu.getWidgetCount() > 0) {
      accountMenu.add(createSeparator());
    }

    accountMenu.add(point);
    point.setStylePrimaryName(TOP_MENU_POINT_STYLE);

  }

  /**
   * Let you add clickable top menu points
   * 
   * @param point
   */
  public void addMenuPoint(Href point) {
    addMenuWidget(point);
  }

  private Label createSeparator() {
    final Label separator = new Label("|");
    separator.addStyleName(TOP_MENU_POINT_STYLE);
    return separator;
  }

}
